package com.xzy.servlet;

import com.xzy.domain.User;
import com.xzy.service.IUserService;
import com.xzy.service.impl.UserServiceImpl;
import com.xzy.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * 继承BaseServlet后可直接编写业务代码而不需要重写service方法
 * 需要调用该方法只需要将方法名通过参数传入即可
 *{@link com.xzy.servlet.BaseServlet}
 */

@WebServlet("/UserServlet")


public class UserServlet extends BaseServlet {
    private IUserService service = new UserServiceImpl();



    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        if (service.login(new User(null, username, password, null)) == null) {
            //设置登录失败后提示信息和回显数据
            req.setAttribute("errorMas", "用户名或密码错误");
            req.setAttribute("userNameBack", username);
            //登录失败返回登录页面
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req, resp);
        } else {
            //保存登录成功信息
            req.getSession().setAttribute("username",username);
            //登录成功转到成功页面
           // req.getRequestDispatcher("/pages/user/login_success.jsp").forward(req, resp);
            resp.sendRedirect(req.getContextPath()+"/pages/user/login_success.jsp");
        }

    }

    protected void regist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String code = req.getParameter("code");
        //使用工具类直接将req中参数注入user对象中(其中user对象属性名与map的key值一一对应)
        User user = WebUtils.copyParametToBean(new User(), req.getParameterMap());
        System.out.println(code);
        //检查验证码
        if (code.equalsIgnoreCase(code)) {
            //检查用户名
            if (service.userNameIsAble(user.getUsername()) == true) {
                //注册成功
                service.registUser(user);
                //跳转到成功页面
               // req.getRequestDispatcher("/pages/user/regist_success.jsp").forward(req, resp);
                req.getSession().setAttribute("username",user.getUsername());
                resp.sendRedirect(req.getContextPath()+"/pages/user/regist_success.jsp");
            } else {
                //设置错误信息
                req.setAttribute("errorMas", "用户名不可用");
                req.setAttribute("userNameBack", user.getUsername());
                req.setAttribute("emaliBack", user.getEmail());
                //用户名不可用

                req.getRequestDispatcher("/pages/user/regist.jsp").forward(req, resp);
            }
        } else {
            //设置错误信息
            req.setAttribute("errorMas", "验证码错误");
            req.setAttribute("userNameBack", user.getUsername());
            req.setAttribute("emaliBack", user.getEmail());
            //验证码错误跳回注册页面
            req.getRequestDispatcher("/pages/user/regist.jsp").forward(req, resp);
        }


    }

    /**
     * 注销登录
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void cancel(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        //清除session
        req.getSession().invalidate();
        //重定向到首页
        resp.sendRedirect(req.getContextPath());
    }

}

